package com.demo.studentmanager;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.demo.studentmanager.databinding.ActivityMainBinding;
import com.example.applicationdemo.R;

public class MineFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_main, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ActivityMainBinding binding = ActivityMainBinding.bind(view);
        binding.switchManager.setChecked(LoginConfig.isManage());
        String phone = LoginConfig.getPhone();
        if (LoginConfig.isManage()) {
            binding.tvAddLesson.setVisibility(View.VISIBLE);
            binding.tvAddStudent.setVisibility(View.VISIBLE);
        } else {
            binding.tvAddLesson.setVisibility(View.GONE);
            binding.tvAddStudent.setVisibility(View.GONE);
        }
        binding.switchManager.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                LoginConfig.setManage(isChecked);
                if (isChecked) {
                    binding.tvAddLesson.setVisibility(View.VISIBLE);
                    binding.tvAddStudent.setVisibility(View.VISIBLE);
                } else {
                    binding.tvAddLesson.setVisibility(View.GONE);
                    binding.tvAddStudent.setVisibility(View.GONE);
                }
            }
        });
        binding.tvAddStudent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(requireActivity(), RegisterActivity.class));
            }
        });
        binding.tvAddLesson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(requireActivity(), EditCourseActivity.class));
            }
        });
        DBHelper dbHelper = new DBHelper(this);
        User user = dbHelper.getUserByPhone(phone);
        binding.etPhone.setText(user.getAccount());
        binding.etAccount.setText(user.getName());
        binding.etClass.setText(user.getClazz());
        binding.etSex.setText(user.getSid());
        binding.tvEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(requireActivity(), EditStudentInfoActivity.class)
                        .putExtra("self", true)
                        .putExtra("phone", phone));
            }
        });
        binding.btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(requireActivity())
                        .setTitle("注意")
                        .setMessage("确定要退出登录吗？")
                        .setPositiveButton("取消", null)
                        .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(requireActivity(), "退出成功！", Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(requireActivity(), LoginActivity.class));

                            }
                        }).show();
            }
        });
    }
}